<template>
  <svg
    :class="svgClass"
    :style="svgStyle"
    aria-hidden="true"
    v-bind="$attrs"
  >
    <use :xlink:href="iconName" />
  </svg>
</template>

<script setup lang="ts">
import { computed } from "vue"

interface Props {
  name: string
  color?: string
  size?: string | number
  className?: string
}

const props = withDefaults(defineProps<Props>(), {
  color: "",
  size: "1em",
  className: ""
})

const iconName = computed(() => `#icon-${props.name}`)

const svgClass = computed(() => {
  const classes = ["svg-icon"]
  if (props.className) {
    classes.push(props.className)
  }
  return classes.join(" ")
})

const svgStyle = computed(() => {
  const style: Record<string, string> = {}
  if (props.color) {
    style.color = props.color
  }
  if (props.size) {
    style.fontSize = typeof props.size === "number" ? `${props.size}px` : props.size
  }
  return style
})
</script>

<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>
